package jsonParse

import com.esri.core.geometry.{Geometry, GeometryEngine, MapGeometry}
import org.json4s.JsonAST.JObject
import org.json4s.NoTypeHints
import org.json4s.jackson.Serialization

/**
 * ClassName: Features <br/>
 * Description: <br/>
 * date: 2020/8/18 13:44<br/>
 *
 * @author Hesion<br/>
 * @version
 * @since JDK 1.8
 */
/*
{
	"type": "FeatureCollection",
	"features": [
    {
      "type": "Feature",
      "id": 0,
      "properties": {
        "boroughCode": 5,
        "borough": "Staten Island",
        "@id": "http:\/\/nyc.pediacities.com\/Resource\/Borough\/Staten_Island"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [-74.050508064032471, 40.566422034160816],
            [-74.049983525625748, 40.566395924928273]
          ]
        ]
      }
    }
  ]
}
 */

case class FeatureCollection(
                              features: List[Feature]
                            )

case class Feature(
                    id: String,
                    properties:Map[String,String],
                    geometry:JObject//不解析内容，交由第三方的工具解析
                  ){
  def getGeometry():Geometry={
    import org.json4s._
    import org.json4s.jackson.JsonMethods._
    val mapGeo: MapGeometry = GeometryEngine.geoJsonToGeometry(compact(render(geometry)), 0, Geometry.Type.Unknown)
    mapGeo.getGeometry()
  }
}
case class FeatureProperties(boroughCode: Int, borough: String)
object FeatureExtraction{
  //完成具体的json转换操作
  def parseJson(json:String): FeatureCollection ={
    import org.json4s.jackson.Serialization.read
      //导入一个隐式转换
    implicit val format =Serialization.formats(NoTypeHints)
    //2.Json->Object
    val featureCollection: FeatureCollection = read[FeatureCollection](json)
    featureCollection
  }
}